<template>
  <div>
    <div class="imgDiv">
      <span>头像</span>
      <div @click="uploadingHead">
        <p v-if="!isImgShow">+</p>
        <img v-else :src="avatar" alt mode="scaleToFill">
      </div>
    </div>
    <div class="nameDiv">
      <span>昵称</span>
      <input type="text" placeholder="请输入虚拟昵称" v-model="nickname">
    </div>
    <button class="btn" @click="addVirtualClick">添加</button>
  </div>
</template>

<script>
import { UPLOAD_IMAGE2, ADD_CUSTOMER_VIRTUAL } from '@/api'

export default {
  components: {},
  data () {
    return {
      avatar: '',
      nickname: '',
      isImgShow: false
    }
  },
  computed: {
    account () {
      return this.$cookie.get('account')
    }
  },
  onShow () {
    this.init()
  },
  created () {},
  methods: {
    uploadingHead () {
      const vm = this
      UPLOAD_IMAGE2()
        .then(r => {
          let res = JSON.parse(r.data)
          if (res.code === '0') {
            vm.avatar = res.data.imgPath

            if (vm.avatar) {
              vm.isImgShow = true
            } else {
              vm.isImgShow = false
            }
          } else {
            vm.$toast(res.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    },
    addVirtualClick () {
      const vm = this
      vm._addVirtual()
    },
    _addVirtual () {
      const vm = this
      let params = {
        avatar: vm.avatar,
        nickname: vm.nickname,
        shopId: vm.account.shopId
      }
      ADD_CUSTOMER_VIRTUAL(params)
        .then(r => {
          if (r.data.code === '0') {
            vm.$alert('添加虚拟用户成功', '').then(() => {
              vm.$router.go(-1)
            })
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    },
    init () {
      const vm = this
      vm.avatar = ''
      vm.nickname = ''
      vm.isImgShow = false
    }
  }
}
</script>

<style lang="scss">
body {
  background: #edeef3;
}
.imgDiv {
  width: 100%;
  height: 100px;
  display: flex;
  align-items: center;
  background: #fff;
  span {
    color: #000;
    font-size: 14px;
    margin: 0 20px;
  }
  p {
    width: 60px;
    height: 60px;
    border: 1px dashed #acacac;
    border-radius: 4px;
    text-align: center;
    line-height: 60px;
    font-size: 30px;
    color: #acacac;
  }
  img {
    width: 60px;
    height: 60px;
    border-radius: 4px;
  }
}
.nameDiv {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  background: #fff;
  margin-top: 5px;
  span {
    color: #000;
    font-size: 14px;
    margin: 0 20px;
  }
  input {
    font-size: 14px;
    color: #000;
    border: 0;
  }
}
.btn {
  width: 90%;
  height: 40px;
  color: #fff;
  background: #5d93fd;
  border-radius: 25px;
  font-size: 16px;
  margin: 100px auto;
  border: 0;
}
</style>

